var table, params, $, ai = 0;
layui.use(['jquery','form','table','iconPicker'], function(){
    $ = jQuery = layui.$;
    var form = layui.form;
    var iconPicker = layui.iconPicker;
    iconPicker.render({
        elem: '#iconPicker',
        type: 'fontClass',
        search: true,
        click: function (data) {
            console.log(data);
        }
    });
    iconPicker.checkIcon('iconPicker', '${item.iconcls!}');
    //var table = layui.table;
    //var formSelects = layui.formSelects;

    /*formSelects.data('column_type', 'server', {
        url: ctx+'/sys_admin/sys/dict/getData',
        keyword: 'columntype',
        success: function(id, url, searchVal, result){
            formSelects.value('column_type', ['varchar']);
            if ('${column.data_type!}' == 'text' || '${column.data_type!}' == 'longtext') {
              $('#lengthid').val('');
                 $('#lengthid').attr('lay-verify', '');
               $('#lengthdivid').hide();
             } else {
                if ('${column.data_type!}' == 'double') {
                  $('#decimalid').val('${column.decimal!}');
                     $('#decimalid').attr('lay-verify', 'required|number');
                   $('#decimaldivid').show();
                 }
             }
        }
    });*/

    /*formSelects.on('column_type', function(id, vals, val, isAdd, isDisabled){
           $('#decimalid').val('');
           if (val.value == 'text' || val.value == 'longtext') {
               $('#lengthid').val('');
               $('#lengthid').attr('lay-verify', '');
             $('#lengthdivid').hide();
           } else {
               $('#lengthid').val('');
               $('#lengthid').attr('lay-verify', 'required|number');
             $('#lengthdivid').show();
               if (val.value == 'double') {
                     $('#decimalid').attr('lay-verify', 'required|number');
                   $('#decimaldivid').show();
               } else {
                     $('#decimalid').attr('lay-verify', '');
                   $('#decimaldivid').hide();
               }
           }
    });*/

    form.on('select(column_type)', function(data){
        if (data.value == 'select') {
            getForeignTables('foreignTableid');
        } else {
            $('#foreignTableid').empty().append('<option value="">无</option>>');
            $('#foreignKeyid').empty().append('<option value="">无</option>>');
        }
        form.render();
    });

    form.on('select(foreignTable)', function(data){
        getForeignKeys('foreignKeyid', data.value);
        form.render();
    });

    $('#addColumn').on('click', function(){
        ai++;
        $('#columnsbodyid').append('<tr>'+
            '<td>'+
            '<input name="column_comment" lay-verify="required" placeholder="请输入字段名称" autocomplete="off" class="layui-input"/>'+
            '</td>'+
            '<td>'+
            '<select name="column_type" lay-filter="column_type'+ai+'">'+
            addoption +
            '</select>'+
            '</td>'+
            '<td>'+
            '<select id="foreignTableid'+ai+'" name="foreignTable" lay-filter="foreignTable'+ai+'">'+
            '<option value="">无</option>'+
            '</select>'+
            '</td>'+
            '<td>'+
            '<select id="foreignKeyid'+ai+'" name="foreignKey">'+
            '<option value="">无</option>'+
            '</select>'+
            '</td>'+
            '<td>'+
            '<input type="number" name="length" lay-verify="" placeholder="请输入字段长度" autocomplete="off" class="layui-input">'+
            '</td>'+
            '<td>'+
            '<input type="number" name="decimal" lay-verify="" placeholder="请输入小数点位" autocomplete="off" class="layui-input">'+
            '</td>'+
            '<td>'+
            '<input name="column_default" lay-verify="" placeholder="请输入默认值" autocomplete="off" class="layui-input"/>'+
            '</td>'+
            '<td>'+
            '<select name="is_nullable">' +
            '  <option value="YES" selected>是</option>' +
            '  <option value="NO">否</option>' +
            '</select>'+
            '</td>'+
            '<td>'+
            '<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteRow(this)">删除</button>'+
            '</td>'+
            '</tr>');
        //formSelects.render();
        form.on('select(column_type'+ai+')', function(data){
            if (data.value == 'select') {
                getForeignTables('foreignTableid'+ai);
            } else {
                $('#foreignTableid'+ai).empty().append('<option value="">无</option>>');
                $('#foreignKeyid'+ai).empty().append('<option value="">无</option>>');
            }
            form.render();
        });
        form.on('select(foreignTable'+ai+')', function(data){
            getForeignKeys('foreignKeyid'+ai, data.value);
            form.render();
        });
        form.render();
        return false;
    });

    form.on('submit', function(data){
        $.post(ctx+'/sys_admin/modules/save', $(data.form).serialize(), function(result) {
            if (result.success) {
                if (params.name == undefined) {
                    table.reload({});
                } else {
                    table.reload({
                        where:{
                            name : params.name,
                            status : params.status
                        },
                        page: {
                            curr: params.curr
                        }
                    });
                }
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
        return false;
    });

});

function deleteRow(obj){
    var index = $(obj).parents("tr").index(); //这个可获取当前tr的下标
    $(obj).parents("tr").remove();
    return false;
}

function getForeignTables(id) {
    $.ajax({
        type: "POST",
        url: ctx+'/sys_admin/modules/getForeignTables',
        async: false,
        success: function (data) {
            $('#'+id).empty();
            $.each(data, function(i, module){
                $('#'+id).append('<option value="'+module.id+'">'+module.name+'</option>>');
            });
        }
    });
}

function getForeignKeys(id, foreignmoduleid) {
    $.ajax({
        type: "POST",
        url: ctx+'/sys_admin/modules/getColumnsList',
        data: {'moduleid': foreignmoduleid, 'page':1, 'limit':100},
        async: false,
        success: function (data) {
            $('#'+id).empty();
            $.each(data.data, function(i, column){
                $('#'+id).append('<option value="'+column.column_name+'">'+column.column_comment+'</option>>');
            });
        }
    });
}